package com.right.health.app.ui.me;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.CollectionUtil;
import com.magic.basic.widget.refresh.OnLastItemVisibleListener;
import com.magic.basic.widget.refresh.SwipeListRefreshLayout;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.AppUserResponse;
import com.right.health.app.bean.ChooseMemberInfo;
import com.right.health.app.bean.FamilyResponse;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.net.FamilyApi;
import com.right.health.app.ui.main.fragment.MedicalFragment;
import com.right.health.app.ui.me.adapter.FamilyAdapter;

import java.util.ArrayList;
import java.util.List;

import static com.right.health.app.constants.BroadCastCode.REFRESH_CHOOSE_FAMILY;
import static com.right.health.app.constants.BroadCastCode.REFRESH_MEDICAL;

/*
 * Created by hopeliao on 2019-09-29
 *
 */
public class FamilyActivity extends BaseActivity implements View.OnClickListener, OnHttpListener, OnLastItemVisibleListener, SwipeRefreshLayout.OnRefreshListener {

    private static final int HTTP_CODE_REFRESH = 1;
    private static final int HTTP_CODE_LOAD_MORE = 2;

    private static final String EXTRA_KEY = "extra:isChoose";

    private static final String EXTRA_KEY_ISSENDBROADCAST = "extra:isSendBrordcast";

    private FamilyAdapter mAdapter;

    private boolean mIsChoose;

    private FamilyApi mFamilyApi;

    private SwipeListRefreshLayout mRefreshView;

    private int mPageNo;

    private View mEmptyView;

    private List<FamilyResponse.FamilyInfo> mDataSource = new ArrayList<>();

    private FamilyResponse.FamilyInfo mMyInfo;

    private boolean mIsSendBroadcast = true;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.family);
    }

    @Override
    public void findView() {
        mEmptyView = findViewById(R.id.empty_view);
        mRefreshView = (SwipeListRefreshLayout) findViewById(R.id.refresh_view);
        mRefreshView.setLastItemVisibleListener(this);
        mRefreshView.setOnRefreshListener(this);

        ListView listView = (ListView) findViewById(R.id.listview);

        mAdapter = new FamilyAdapter(this);
        listView.setAdapter(mAdapter);

        listView.setOnItemClickListener((parent, view, position, id) -> {
            FamilyResponse.FamilyInfo info = (FamilyResponse.FamilyInfo) parent.getItemAtPosition(position);
            if (info != null) {
                if (mIsChoose) {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putParcelable(BroadCastKey.DATA, info);
                    intent.putExtras(bundle);

                    if(mIsSendBroadcast) {
                        sendBroadcast(REFRESH_CHOOSE_FAMILY);
                        App.getInstance().refreshChooseMember(new ChooseMemberInfo(info.membername, info.memberid, info.sex));
                    }
                    sendBroadcast(REFRESH_MEDICAL);
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putParcelable(BroadCastKey.DATA, info);
                    startIntent(FamilyDetailActivity.class, bundle);
                }
            }

        });

        findViewById(R.id.ib_add).setOnClickListener(this);
        findViewById(R.id.tv_goto_add).setOnClickListener(this);
    }

    @Override
    public void initialize() {

        mFamilyApi = new FamilyApi();
        mFamilyApi.setListener(this);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null && bundle.containsKey(EXTRA_KEY)) {
            mIsChoose = bundle.getBoolean(EXTRA_KEY, false);
            mIsSendBroadcast = bundle.getBoolean(EXTRA_KEY_ISSENDBROADCAST, true);
        }
        initMyInfo();
        mRefreshView.setRefreshing(true);
        onRefresh();
//        mAdapter.setDataSource(DataCommon.getTestDataSource());
    }

    private void initMyInfo() {
        mMyInfo = new FamilyResponse.FamilyInfo();
        AppUserResponse.AppUserInfo user= App.getInstance().getUser();
        mMyInfo.memberid = user.memberid;
        mMyInfo.membername = "本人";
        mMyInfo.membership = "2";
        mMyInfo.memberidcard = TextUtils.isEmpty(user.idcard)?"未知":user.idcard;
        mMyInfo.patientidcardnum = user.patientidcardnum;
        mMyInfo.sex = user.sex;
        mMyInfo.healthcardno = user.healthcardno;
        mMyInfo.memberphone = user.mobilephone;
        mDataSource.add(mMyInfo);
        mAdapter.setDataSource(mDataSource);
    }

    @Override
    public void onReceiveBroadcast(int intent, Bundle bundle) {
        super.onReceiveBroadcast(intent, bundle);
        if (intent == BroadCastCode.REFRESH_FAMILY) {
            mRefreshView.setRefreshing(true);
            onRefresh();
        }
    }

    public static void makeActivity(Fragment fragment, boolean isChoose, boolean isSendBroadcast, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), FamilyActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean(EXTRA_KEY, isChoose);
        bundle.putBoolean(EXTRA_KEY_ISSENDBROADCAST, isSendBroadcast);
        intent.putExtras(bundle);

        fragment.startActivityForResult(intent, requestCode);
    }

    public static void makeActivity(Fragment fragment, boolean isChoose, int requestCode) {
        makeActivity(fragment, isChoose, true, requestCode);
    }

    public static void makeActivity(Activity act, boolean isChoose, int requestCode) {
        makeActivity(act, isChoose, true, requestCode);
    }

    public static void makeActivity(Activity act, boolean isChoose, boolean isSendBroadcast, int requestCode) {
        Intent intent = new Intent(act, FamilyActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean(EXTRA_KEY, isChoose);
        bundle.putBoolean(EXTRA_KEY_ISSENDBROADCAST, isSendBroadcast);
        intent.putExtras(bundle);

        act.startActivityForResult(intent, requestCode);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ib_add) {
            startIntent(FamilyAddActivity.class);
        } else if (v.getId() == R.id.tv_goto_add) {
            startIntent(FamilyAddActivity.class);
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        mRefreshView.onRefreshComplete();
        if (response.isResponseOK()) {
            if (response.targetData instanceof FamilyResponse) {
                FamilyResponse familyResponse = response.cast(FamilyResponse.class);
                if (request.requestCode == HTTP_CODE_REFRESH) {
                    mDataSource.clear();
                    mDataSource.add(mMyInfo);
                }

                mDataSource.addAll(familyResponse.members);

                if (familyResponse.page != null) {
                    mRefreshView.setCanLoadMore(!familyResponse.page.lastPage);
                }
                mEmptyView.setVisibility(CollectionUtil.isEmpty(mDataSource) ? View.VISIBLE : View.GONE);
                mAdapter.setDataSource(mDataSource);
            }
        } else {
            mEmptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onLastItemVisible() {
        mPageNo++;
        mFamilyApi.addRequestCode(HTTP_CODE_LOAD_MORE);
        mFamilyApi.getFamilyList(mPageNo);
    }

    @Override
    public void onRefresh() {
        mPageNo = 1;
        mFamilyApi.addRequestCode(HTTP_CODE_REFRESH);
        mFamilyApi.getFamilyList(mPageNo);
    }
}
